package com.co.video.service.impl;

import com.co.video.mapper.DetailMapper;
import com.co.video.pojo.Detail;
import com.co.video.service.DetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * TvVdetail业务层接口实现类
 */
@Service

public class DetailServiceImpl implements DetailService {

    @Autowired
    private DetailMapper detailMapper;


    /**
     * TvVdetail条件+分页查询
     *
     * @param detail 查询条件
     * @param page   页码
     * @param size   页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Detail> findPage(Detail detail, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(detail);
        //执行搜索
        return new PageInfo<Detail>(detailMapper.selectByExample(example));
    }

    /**
     * TvVdetail分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Detail> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Detail>(detailMapper.selectAll());
    }

    /**
     * TvVdetail条件查询
     *
     * @param detail
     * @return
     */
    @Override
    public List<Detail> findList(Detail detail) {
        //构建查询条件
        Example example = createExample(detail);
        //根据构建的条件查询数据
        return detailMapper.selectByExample(example);
    }


    /**
     * TvVdetail构建查询对象
     *
     * @param detail
     * @return
     */
    public Example createExample(Detail detail) {
        Example example = new Example(Detail.class);
        Example.Criteria criteria = example.createCriteria();
        if (detail != null) {
            // av号 主键
            if (!StringUtils.isEmpty(detail.getAid())) {
                criteria.andEqualTo("aid", detail.getAid());
            }
            // 视频名称
            if (!StringUtils.isEmpty(detail.getTitle())) {
                criteria.andLike("title", "%" + detail.getTitle() + "%");
            }
            // 视频缩略图地址
            if (!StringUtils.isEmpty(detail.getImage())) {
                criteria.andEqualTo("Image", detail.getImage());
            }
            // 视频真实url
            if (!StringUtils.isEmpty(detail.getUrl())) {
                criteria.andEqualTo("url", detail.getUrl());
            }
            // 视频上传时间
            if (!StringUtils.isEmpty(detail.getUploadTime())) {
                criteria.andEqualTo("uploadTime", detail.getUploadTime());
            }
            // 视频所属组群(分类id)
            if (!StringUtils.isEmpty(detail.getCategoryId())) {
                criteria.andEqualTo("categoryId", detail.getCategoryId());
            }
            // 视频总时常
            if (!StringUtils.isEmpty(detail.getDurations())) {
                criteria.andEqualTo("durations", detail.getDurations());
            }
            // 视频审核状态 0为下架inactive 1为上架active
            if (!StringUtils.isEmpty(detail.getStatus())) {
                criteria.andEqualTo("status", detail.getStatus());
            }
            // up主关联username
            if (!StringUtils.isEmpty(detail.getUsername())) {
                criteria.andLike("username", "%" + detail.getUsername() + "%");
            }
            // 是否上传成功 0为fail 1为successful
            if (!StringUtils.isEmpty(detail.getIsUpload())) {
                criteria.andEqualTo("isUpload", detail.getIsUpload());
            }
            // 视频简介
            if (!StringUtils.isEmpty(detail.getComment())) {
                criteria.andEqualTo("user", detail.getComment());
            }
            // 该视频有多少P
            if (!StringUtils.isEmpty(detail.getVideos())) {
                criteria.andEqualTo("videos", detail.getVideos());
            }
            // 创建时间
            if (!StringUtils.isEmpty(detail.getCreateTime())) {
                criteria.andEqualTo("createTime", detail.getCreateTime());
            }
            // 更新时间
            if (!StringUtils.isEmpty(detail.getUpdateTime())) {
                criteria.andEqualTo("updateTime", detail.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        detailMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改TvVdetail
     *
     * @param detail
     */
    @Override
    public void update(Detail detail) {
        detailMapper.updateByPrimaryKey(detail);
    }

    /**
     * 增加TvVdetail
     *
     * @param detail
     */
    @Override
    public void add(Detail detail) {
        detailMapper.insert(detail);
    }

    /**
     * 根据ID查询TvVdetail
     *
     * @param aid
     * @return
     */
    @Override
    public Detail findById(String aid) {
        return detailMapper.selectByPrimaryKey(aid);
    }

    /**
     * 根据用户名查询上传的视频
     * @param id
     * @return
     */
    @Override
    public Detail findByUsername(String id) {

        return detailMapper.selectByPrimaryKey(id);

    }

    /**
     * 查询TvVdetail全部数据
     *
     * @return
     */
    @Override
    public List<Detail> findAll() {
        return detailMapper.selectAll();
    }

    @Override
    public Detail findByTitle(String title) {
        Detail detail = new Detail();
        detail.setTitle(title);
        return detailMapper.selectOne(detail);
    }

    /**
     * 根据用户名查询用户上传的视频
     * @param username
     * @return
     */
    @Override
    public List<Map> findListByUsername(String username) {
        List<Map> listByUsername = detailMapper.findListByUsername(username);
        return listByUsername;
    }
}
